package com.system.seckillgoods.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.bean.SeckillActivity;
import com.system.common.util.Result;
import com.system.common.service.ISeckillActivityService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/seckillactivity")
public class SeckillActivityController {

    @DubboReference
    private ISeckillActivityService activityService;

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        return Result.ok("查询成功", activityService.getActivityById(id));
    }

    @PostMapping("/add")
    public Result add(@RequestBody SeckillActivity activity) {
        activityService.addActivity(activity);
        return Result.ok("新增活动成功");
    }

    @PutMapping("/update")
    public Result update(@RequestBody SeckillActivity activity) {
        activityService.updateActivity(activity);
        return Result.ok("更新活动成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        activityService.deleteActivity(id);
        return Result.ok("删除活动成功");
    }

    @GetMapping("/list")
    public Result list() {
        List<SeckillActivity> list = activityService.getActivityList();
        return Result.ok("查询活动列表成功", list);
    }

    @GetMapping("/page")
    public Result page(SeckillActivity activity,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.ok("分页查询活动成功",
                activityService.getPage(activity, new Page<>(pageNum, pageSize)));
    }

    @GetMapping("/isActive/{id}")
    public Result isActive(@PathVariable Integer id) {
        return Result.ok("查询活动状态成功", activityService.isActivityActive(id));
    }
}
